package com.guojian.jdbc;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.JdbcProperties;
import org.springframework.jdbc.core.RowMapper;

import com.guojian.jdbc.jpa.Test;
import com.guojian.jdbc.mapper.RowMapperTemplate;

//@Configuration
public class JdbcTemplateXAutoConfiguration {

	//@Bean
	//@Primary
	public JdbcTemplateX jdbcTemplateX(DataSource dataSource, JdbcProperties properties) {
		return new JdbcTemplateX(dataSource, DBType.oracle);
	}

	public static void main(String[] args) {

		test1();
	}

	public static void test4() {
		JdbcTemplateX jdbcx = new JdbcTemplateX(DataSourceFactory.getDataSource("test"));
		RowMapper<Map<String, Object>> mapper = RowMapperTemplate.getMapRowMapper(Object.class, false, null);
		List<Map<String, Object>> sss = jdbcx.select("select * from test_e", mapper);

		for (Map<String, Object> map : sss) {
			System.out.println(map);
		}
	}

	public static void test3() {
		JdbcTemplateX jdbcx = new JdbcTemplateX(DataSourceFactory.getDataSource("test"));
		RowMapper<String> mapper = RowMapperTemplate.builder(String.class).createSingleColumnRowMapper(null);
		List<String> sss = jdbcx.select("select * from test_e", mapper);

		for (String string : sss) {
			System.out.println(string + "----------");
		}
	}

	public static void test2() {
		JdbcTemplateX jdbcx = new JdbcTemplateX(DataSourceFactory.getDataSource("test"));
		RowMapper<Map<String, String>> stringrowmapper = RowMapperTemplate.builder().createStringMapRowMapper(null);
		List<Map<String, String>> a = jdbcx.select("select * from test_e", stringrowmapper);
		for (Map<String, String> map : a) {
			System.out.println(map);
		}
	}

	public static void test1() {

		JdbcTemplateX jdbcx = new JdbcTemplateX(DataSourceFactory.getDataSource("test"));
		RowMapper<Test> rowmaper = RowMapperTemplate.builder(Test.class).createBeanPropertyRowMapper(new JdbcDataFormat<Test>() {
			@Override
			public void formatRow(Test row) {
				row.setId(row.getId() + "==========");
			}
		});

		/*List<Test> a = jdbcx.select("select * from test_e", rowmaper);
		for (Test test : a) {
			System.out.println(test);
		}*/

		Test test = new Test();

		test.setId("ndu0bN1Zhwhng");
		test.setName("guojian");
		//test.setBeginDate("2021-06-01");

		List<Test> test111List = jdbcx.selectListByEntity(test, Test.class);

		for (Test test2 : test111List) {
			System.out.println(test2);
		}

		/*int effectRows = jdbcx.deleteByEntity(test);
		
		System.out.println(effectRows);*/

	}

}
